package com.diu.domain.strategy.service.rule.filter.impl;

import com.diu.domain.strategy.model.entity.RuleActionEntity;
import com.diu.domain.strategy.model.entity.RuleMatterEntity;
import com.diu.domain.strategy.model.vo.RuleLogicCheckTypeVO;
import com.diu.domain.strategy.repository.IStrategyRepository;
import com.diu.domain.strategy.service.annotation.LogicStrategy;
import com.diu.domain.strategy.service.rule.filter.ILogicFilter;
import com.diu.domain.strategy.service.rule.filter.factory.DefaultLogicFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author #diu
 * @description #用户抽奖n次后，对应奖品可解锁抽奖
 * @create #2025/10/12 15:48
 */
@Slf4j
@Component
@LogicStrategy(logicMode = DefaultLogicFactory.LogicModel.RULE_LOCK)
public class RuleLockLogicFilter implements ILogicFilter<RuleActionEntity.RaffleMiddleEntity> {
    @Resource
    private IStrategyRepository repository;

    private Long userRaffleCount = 0L;
    @Override
    public RuleActionEntity<RuleActionEntity.RaffleMiddleEntity> filter(RuleMatterEntity ruleMatterEntity) {
        log.info("锁定规则过滤器触发，userId:{} strategyId:{} ruleModel:{}", ruleMatterEntity.getUserId(), ruleMatterEntity.getStrategyId(), ruleMatterEntity.getRuleModel());

        String ruleValue = repository.queryStrategyRuleValue(ruleMatterEntity.getStrategyId(), ruleMatterEntity.getAwardId(), ruleMatterEntity.getRuleModel());
        long raffleCount = Long.parseLong(ruleValue);

        if(userRaffleCount >= raffleCount){
            return RuleActionEntity.<RuleActionEntity.RaffleMiddleEntity>builder()
                    .code(RuleLogicCheckTypeVO.ALLOW.getCode())
                    .info(RuleLogicCheckTypeVO.ALLOW.getInfo())
                    .ruleModel(DefaultLogicFactory.LogicModel.RULE_LOCK.getCode())
                    .data(RuleActionEntity.RaffleMiddleEntity.builder()
                            .middleState("解锁抽奖")
                            .build())
                    .build();

        }
        return RuleActionEntity.<RuleActionEntity.RaffleMiddleEntity>builder()
                .code(RuleLogicCheckTypeVO.TAKE_OVER.getCode())
                .info(RuleLogicCheckTypeVO.TAKE_OVER.getInfo())
                .ruleModel(DefaultLogicFactory.LogicModel.RULE_LOCK.getCode())
                .data(RuleActionEntity.RaffleMiddleEntity.builder()
                        .middleState("锁定抽奖")
                        .build())
                .build();
    }
}
